package com.example.accounting.slice;

import com.example.accounting.ResourceTable;
import com.example.accounting.model.AccountInfo;
import com.example.accounting.model.AccountOperate;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PropertySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_property);

        DirectionalLayout directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_property_account);
        directionalLayout.removeAllComponents();
        AccountOperate accountOperate = new AccountOperate();
        List<AccountInfo> accountInfos = accountOperate.selectAll();

        Map<String, Double> map = new HashMap<>();
        double property = 0;
        for (AccountInfo accountInfo : accountInfos) {
            String account = accountInfo.getAccount();
            property += accountInfo.getMoney();
            if (map.containsKey(account)) {
                map.put(accountInfo.getAccount(), accountInfo.getMoney() + map.get(account));
            } else {
                map.put(accountInfo.getAccount(), accountInfo.getMoney());
            }
        }

        Text text = (Text) findComponentById(ResourceTable.Id_property_now);
        text.setText(String.valueOf(property));

        for (String str : map.keySet()) {

            Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_property_info, null, false);

            Text property_info_account = (Text) container.findComponentById(ResourceTable.Id_property_info_account);
            Text property_info_money = (Text) container.findComponentById(ResourceTable.Id_property_info_money);

            property_info_account.setText(str);
            property_info_money.setText(String.valueOf(map.get(str)));

            directionalLayout.addComponent(container);
        }


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
